package com.kkb.service.withdrawalmanager.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.bean.WithdrawalTable;
import com.kkb.dao.WithdrawalTableMapper;
import com.kkb.service.withdrawalmanager.WithdrawalManagerService;
import com.kkb.vo.profitmanager.req.SelectOverviewReq;
import com.kkb.vo.profitmanager.req.SelectWithdrawalReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WithdrawalManagerServiceImpl implements WithdrawalManagerService {
    @Autowired
    private WithdrawalTableMapper withdrawalTableMapper;

    @Override
    public PageInfo<WithdrawalTable> selectWithdrawal(SelectWithdrawalReq param) {
        //创建tkmybatis查询参数
        WithdrawalTable withdrawal = new WithdrawalTable();
        withdrawal.setSellerId(param.getSellerId());
        //1.指定分页数据
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        //2.查询数据
        List<WithdrawalTable> list = withdrawalTableMapper.select(withdrawal);
        if (list.size()!=0){
            for (WithdrawalTable w:list){
                //遍历list，通过每个Withdrawal对象的time值为timeInterval属性设值
                Date withdrawalTime = w.getWithdrawalTime();
                String[] strDate = new SimpleDateFormat("yyyy-MM-dd").format(withdrawalTime).toString().split("-");
                int year = Integer.parseInt(strDate[0]);
                int month = Integer.parseInt(strDate[1]);
                if (month==12){
                    w.setWithdrawalTimeInterval(year+"-"+month+"至"+(year+1)+"-"+1);
                }else {
                    w.setWithdrawalTimeInterval(year+"-"+month+"至"+year+"-"+(month+1));
                }
            }
        }
        //3.创建分页工具类
        PageInfo<WithdrawalTable> withdrawalTablePageInfo = new PageInfo<>(list);
        return withdrawalTablePageInfo;
    }

    @Override
    public BigDecimal selectWithdrawnAmount(SelectOverviewReq param) {
        return withdrawalTableMapper.selectWithdrawnAmount(param);
    }
}
